package com.situ.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.crm.entity.User;
import com.situ.crm.entity.UserRole;
import com.situ.crm.mapper.RoleMapper;
import com.situ.crm.mapper.UserMapper;
import com.situ.crm.mapper.UserRoleMapper;
import com.situ.crm.service.IRoleService;
import com.situ.crm.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.crm.util.LayUITableResult;
import com.situ.crm.vo.LayUISelectMVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Gao
 * @since 2021-11-03
 */
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private IRoleService roleService;


    @Override
    public List<String> selectRolesByUserName(String name) {
        return userMapper.selectRolesByUserName(name);
    }

    @Override
    public List<String> selectPermissionsByUserName(String name) {
        return userMapper.selectPermissionsByUserName(name);
    }

    @Override
    public LayUITableResult selectByPage(Long page, Long limit) {
        IPage<User> iPage = new Page<>(page, limit);
        IPage<User> result = userMapper.selectPage(iPage, new QueryWrapper<>());

        return LayUITableResult.ok(result.getTotal(), result.getRecords());
    }

    @Override
    public void insert(User user, String roles) {
        // 首先将user插入到User表中
        userMapper.insert(user);
        // 将"用户-角色"多对多关系插入到user_role
        String[] roleIds = roles.split(",");
        for (String roleId : roleIds) {
            UserRole userRole = new UserRole();
            userRole.setUserId(user.getId());
            userRole.setRoleId(Integer.parseInt(roleId));
            userRoleMapper.insert(userRole);
        }
    }

    @Override
    public void deleteById(Integer id) {
        // 先去中间表删除，再去user表删除，不然会有
        // Cannot delete or update a parent row: a foreign key constraint fails
        // delete from user_role where user_id=id;
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", id);
        userRoleMapper.delete(queryWrapper);

        userMapper.deleteById(id);
    }

    @Override
    public Map<String, Object> selectUserAndRoles(Integer userId) {
        Map<String, Object> map = new HashMap<>();
        User user = userMapper.selectById(userId);
        map.put("user", user);
        List<LayUISelectMVO> layUISelectMVOS = roleService.selectAllRoles();
        map.put("allRoles", layUISelectMVOS);

        // select role_id from user_role where user_id=12;
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("role_id");
        queryWrapper.eq("user_id", userId);
        List list = userRoleMapper.selectObjs(queryWrapper);
        map.put("selectIds", list);
        return map;
    }

    @Override
    public void update(User user, String roles) {
        userMapper.updateById(user);

        // 先去user_role表里面把这个user所有角色删除掉
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", user.getId());
        userRoleMapper.delete(queryWrapper);

        String[] roleIds = roles.split(",");
        for (String roleId : roleIds) {
            UserRole userRole = new UserRole();
            userRole.setUserId(user.getId());
            userRole.setRoleId(Integer.parseInt(roleId));
            userRoleMapper.insert(userRole);
        }
    }
}
